<?php

namespace app\common\model;

use app\admin\library\Auth;
use think\Model;

class Classes extends Model
{
    // 连接数据库名称
    protected $connection = 'db2';
    // 表名
    protected $name = 'classes';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;


    public static function getMergeClasses($schools)
    {
        $data = [];

        // 所有年级
        $grades = Grade::column('id,name');
        // 所有班级
        $classes = self::alias('classes')
            ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
            ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
            ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
            ->whereIn('classes.school_id', array_keys($schools))
            ->column('classes.id class_id,classes.name,classes.grade_id,classes.school_id,classes.headmaster headmaster_id,user.nickname headmaster_name,classes.classroom_id,classroom.name classroom_name', 'classes.id');

        // 学校.年级.班级 便于查找
        foreach ($classes as $key => $value) {
            if (isset($schools[$value['school_id']])) {
                if (isset($grades[$value['grade_id']])) {
                    $data["{$schools[$value['school_id']]}-{$grades[$value['grade_id']]}-{$value['name']}"] = $value;
                }
            }
        }

        return $data;
    }

}
